package com.family.web.controller.pedigree;

import cn.hutool.core.util.ObjectUtil;
import com.family.common.annotation.Log;
import com.family.common.core.controller.BaseController;
import com.family.common.core.domain.AjaxResult;
import com.family.common.core.oss.OssFactory;
import com.family.common.core.oss.UploadResult;
import com.family.common.core.page.TableDataInfo;
import com.family.common.enums.BusinessType;
import com.family.common.exception.ServiceException;
import com.family.common.core.oss.OssClient;
import com.family.common.utils.StringUtils;
import com.family.pedigree.domain.FamilyOss;
import com.family.pedigree.service.IFamilyOssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author forever
 * Date 2024/11/24 16:00
 * desc 文件上传
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/family/oss")
public class OssController extends BaseController {

    private final IFamilyOssService ossService;


    /**
     * 上传图片
     */
    @Log(title = "oss文件上传", businessType = BusinessType.OTHER)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult upload(@RequestPart("file") MultipartFile file) {

        if (ObjectUtil.isNull(file)) {
            return AjaxResult.error("上传文件不能为空");
        }
        return AjaxResult.success(ossService.upload(file));
    }

    @GetMapping("/list")
    public TableDataInfo list(FamilyOss familyOss) {
        startPage();
        return getDataTable(ossService.lambdaQuery().like(StringUtils.isNotBlank(familyOss.getFileName()), FamilyOss::getFileName, familyOss.getFileName()).list());
    }

    @Log(title = "oss文件下载", businessType = BusinessType.OTHER)
    @GetMapping("/download/{ossId}")
    public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException {
        ossService.download(ossId, response);
    }

    @Log(title = "oss文件删除", businessType = BusinessType.OTHER)
    @DeleteMapping("/{ossId}")
    public AjaxResult delete(@PathVariable Long ossId) {
        ossService.delById(ossId);
        return AjaxResult.success();
    }
}
